package mysql

import (
	"cloud-storage/global"
	"cloud-storage/utils"
	"strconv"
	"strings"
	"time"
)

type Share struct {
	Id       int
	Code     string
	FileId   int
	Username string
	Hash     string
}

// CreateShare 创建分享
func CreateShare(code, username string, fId int) string {
	share := Share{
		Code:     strings.ToLower(code),
		FileId:   fId,
		Username: username,
		Hash:     utils.EnCodeMD5(code + strconv.Itoa(int(time.Now().Unix()))),
	}
	global.DB.Create(&share)

	return share.Hash
}

// GetShareInfo 查询分享
func GetShareInfo(f string) (share Share) {
	global.DB.Find(&share, "hash = ?", f)
	return
}

// VerifyShareCode 校验提取码
func VerifyShareCode(fId, code string) bool {
	var share Share
	global.DB.Find(&share, "file_id = ? and code = ?", fId, code)
	if share.Id == 0 {
		return false
	}
	return true
}
